package com.dlyk.web;

import com.dlyk.model.TDicType;
import com.dlyk.model.TProduct;
import com.dlyk.query.DicTypeQuery;
import com.dlyk.query.ProductQuery;
import com.dlyk.result.R;
import com.dlyk.service.DicTypeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/8/16 22:42:05
 * @version: 1.0
 */
@RestController
public class DicTypeController {

    @Resource
    private DicTypeService dicTypeService;

    /**
     * @description: 字典类型列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 20:47
     */
    @GetMapping("api/dicType")
    public R dicTypePage(@RequestParam(value = "current",required = false) Integer current){
        if (current == null){
            current = 1;
        }
        PageInfo<TDicType> pageInfo = dicTypeService.getDicTypeByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * @description: 根据id查询字典类型
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:54
     */
    @GetMapping("api/dicType/detail/{id}")
    public R loadDicType(@PathVariable(value = "id") Integer id){
        TDicType tDicType = dicTypeService.getDicTypeById(id);
        return R.OK(tDicType);
    }

    /**
     * @description: 新增字典类型
     * @params: [tDicType]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PostMapping("api/dicType")
    public R addDicType(DicTypeQuery dicTypeQuery){
        int save = dicTypeService.saveDicType(dicTypeQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 编辑字典类型
     * @params: [tDicType]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PutMapping("api/dicType")
    public R editDicType(DicTypeQuery dicTypeQuery){
        int update = dicTypeService.updateDicType(dicTypeQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 根据id删除字典类型
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 18:16
     */
    @DeleteMapping("api/dicType/{id}")
    public R delDicType(@PathVariable(value = "id") Integer id){
        int del = dicTypeService.delDicTypeById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除字典类型
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 18:32
     */
    @DeleteMapping("api/dicType")
    public R batchDelDicType(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = dicTypeService.batchDelDicTypeByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }


}
